<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.qiqianyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\company\controller;

use app\admin\model\RouteModel;
use app\company\model\CompanyCategoryModel;
use cmf\controller\AdminBaseController;
use app\company\model\MemberModel;
use think\Db;
use app\company\service\CategoryService;
use app\company\service\CompanyService;
use app\common\model\DistrictModel;
use app\company\validate\AdminUserValidate;
use app\user\model\UserModel;
use app\company\model\MemberBabyModel;


class AdminMemberBabyController extends AdminBaseController
{

    public function index(){

        $memberBabyModel = new MemberBabyModel();
        $id = $this->request->param('id', 0, 'intval');

        $where = [
            'delete_time'    => 0,
            'user_id'    => $id,
        ];

        $order    = "list_order";

        $babyList = $memberBabyModel->where($where)->order($order)->select();

        $this->assign('baby_list', $babyList);

        return $this->fetch();
    }



    /**
     * 打开添加页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){



        return $this->fetch();
    }
    public function edit(){


        $model = new  MemberBabyModel();
        $id = $this->request->param('id', 0, 'intval');

        $post            = $model->where('id', $id)->find();

        $this->assign('post', $post);
        return $this->fetch();
    }


    public function addPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();



            $post   = $data['post'];

            //dump($data);

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }

            //dump($data);

           $result = $this->validate($data['post'], 'MemberBaby');
            if ($result !== true) {
              $this->error($result);
            }


            $data['post']['create_time'] = time();
            $data['post']['update_time'] = time();
            $data['post']['birthday'] =  strtotime($post["birthday"]);




            $model = new  MemberBabyModel();
              $model->allowField(true)->data($data['post'], true)->isUpdate(false)->save();




            //$memberModel->addMember($data['post']);



            $this->success('保存成功!');

        }

    }



    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();


            $post = $data['post'];

            //dump($data);

            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }

            //dump($data);

            $result = $this->validate($data['post'], 'MemberBaby');
            if ($result !== true) {
                $this->error($result);
            }


            $data['post']['update_time'] = time();
            $data['post']['birthday'] =  strtotime($post["birthday"]);


            $model = new  MemberBabyModel();
            $model->allowField(true)->data($data['post'], true)->isUpdate(true)->save();


            $this->success('保存成功!');
        }
    }


    public function delete()
    {
            $data = $this->request->param();
            $id = $data['id'];

            $data['delete_time']=time();

            $model = Db::name("member_baby");

            $model->where('id', $id)->update($data);

            $this->success('删除成功!');
    }


}
